home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicButtonListener.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  256 lines

  1. /*
  2.  * @(#)BasicButtonListener.java    1.24 98/06/22
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.java.swing.plaf.basic;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.beans.*;
  20. import java.io.Serializable;
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23.  
  24. /**
  25.  * Button Listener
  26.  * <p>
  27.  * Warning: serialized objects of this class will not be compatible with
  28.  * future swing releases.  The current serialization support is appropriate 
  29.  * for short term storage or RMI between Swing1.0 applications.  It will
  30.  * not be possible to load serialized Swing1.0 objects with future releases
  31.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  32.  * baseline for the serialized form of Swing objects.
  33.  *
  34.  * @version 1.24 06/22/98
  35.  * @author Jeff Dinkins 
  36.  * @author Arnaud Weber (keyboard UI support)
  37.  */
  38.  
  39. public class BasicButtonListener implements MouseListener, MouseMotionListener, 
  40.                                    FocusListener, ChangeListener, PropertyChangeListener, Serializable
  41. {
  42.     // used in mouseDragged
  43.     private Rectangle tmpRect = new Rectangle();
  44.  
  45.  
  46.     // Keyboard Actions used by the mnemonic accelerator and
  47.     // the "spacebar" accelerator triggers
  48.     private KeyStroke altPressedKeyStroke = null;
  49.     private KeyStroke altReleasedKeyStroke = null;
  50.     private KeyStroke nonAltReleasedKeyStroke = null;
  51.  
  52.     // These two keystrokes can be shared accross all buttons. 
  53.     private static KeyStroke spacePressedKeyStroke = null;
  54.     private static KeyStroke spaceReleasedKeyStroke = null;
  55.  
  56.     public BasicButtonListener(AbstractButton b) {
  57.     if(spacePressedKeyStroke == null) {
  58.         spacePressedKeyStroke = KeyStroke.getKeyStroke(' ', 0, false);
  59.         spaceReleasedKeyStroke = KeyStroke.getKeyStroke(' ', 0,true);
  60.     }
  61.     }
  62.  
  63.     public void propertyChange(PropertyChangeEvent e) {
  64.     String prop = e.getPropertyName();
  65.     if(prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) {
  66.         uninstallKeyboardActions((AbstractButton) e.getSource());
  67.         installKeyboardActions((AbstractButton) e.getSource());
  68.     }
  69.     }
  70.  
  71.     /**
  72.      * Register default key actions: pressing space to "click" a
  73.      * button and registring the keyboard mnemonic (if any).
  74.      */
  75.     public void installKeyboardActions(AbstractButton b) {
  76.     PressedAction pressedAction = new PressedAction(b);
  77.     ReleasedAction releasedAction = new ReleasedAction(b);
  78.  
  79.     b.registerKeyboardAction(pressedAction, spacePressedKeyStroke, JComponent.WHEN_FOCUSED);
  80.     b.registerKeyboardAction(releasedAction, spaceReleasedKeyStroke, JComponent.WHEN_FOCUSED);
  81.  
  82.     int m = b.getMnemonic();
  83.     if(m != 0) {
  84.         altPressedKeyStroke     = KeyStroke.getKeyStroke(m, ActionEvent.ALT_MASK, false);
  85.         altReleasedKeyStroke    = KeyStroke.getKeyStroke(m, ActionEvent.ALT_MASK, true);
  86.         nonAltReleasedKeyStroke = KeyStroke.getKeyStroke(m, 0, true);
  87.         
  88.         b.registerKeyboardAction(pressedAction, altPressedKeyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  89.         b.registerKeyboardAction(releasedAction, altReleasedKeyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  90.         b.registerKeyboardAction(releasedAction, nonAltReleasedKeyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  91.     } 
  92.     }
  93.  
  94.     /**
  95.      * Unregister's default key actions
  96.      * @see #registerKeyboardActions
  97.      */
  98.     public void uninstallKeyboardActions(AbstractButton b) {
  99.     // Don't null out the spacePressed/spaceReleased KeyStrokes,
  100.     // they are shared accross all buttons
  101.      if(spacePressedKeyStroke != null) {
  102.         b.unregisterKeyboardAction(spacePressedKeyStroke);
  103.     }
  104.  
  105.      if(spaceReleasedKeyStroke != null) {
  106.         b.unregisterKeyboardAction(spaceReleasedKeyStroke);
  107.     }
  108.  
  109.      if(altPressedKeyStroke != null) {
  110.         b.unregisterKeyboardAction(altPressedKeyStroke);
  111.         altPressedKeyStroke = null;
  112.     }
  113.  
  114.      if(altReleasedKeyStroke != null) {
  115.         b.unregisterKeyboardAction(altReleasedKeyStroke);
  116.         altReleasedKeyStroke = null;
  117.     }
  118.  
  119.      if(nonAltReleasedKeyStroke != null) {
  120.         b.unregisterKeyboardAction(nonAltReleasedKeyStroke);
  121.         nonAltReleasedKeyStroke = null;
  122.     }
  123.     }
  124.  
  125.  
  126.     public void stateChanged(ChangeEvent e) {
  127.     AbstractButton b = (AbstractButton) e.getSource();
  128.         b.repaint();
  129.     }
  130.  
  131.     public void focusGained(FocusEvent e) { 
  132.     AbstractButton b = (AbstractButton) e.getSource();
  133.         if (b instanceof JButton) {
  134.             JRootPane root = b.getRootPane();
  135.             if (root != null) {
  136.                 root.setDefaultButton((JButton)b);
  137.             }
  138.         }
  139.     b.repaint();
  140.     }
  141.  
  142.     public void focusLost(FocusEvent e) {
  143.     AbstractButton b = (AbstractButton) e.getSource();
  144.     b.repaint();
  145.     }
  146.  
  147.     public void mouseMoved(MouseEvent e) {
  148.     };
  149.  
  150.  
  151.     public void mouseDragged(MouseEvent e) {
  152.     AbstractButton b = (AbstractButton) e.getSource();
  153.  
  154.     // HACK! We're forced to do this since mouseEnter and mouseExit aren't
  155.     // reported while the mouse is down.
  156.     ButtonModel model = b.getModel();
  157.  
  158.     if(model.isPressed()) {
  159.             tmpRect.width = b.getWidth();
  160.             tmpRect.height = b.getHeight();
  161.             if(tmpRect.contains(e.getPoint())) {
  162.                 model.setArmed(true);
  163.             } else {
  164.                 model.setArmed(false);
  165.             }
  166.         }
  167.     };
  168.  
  169.     public void mouseClicked(MouseEvent e) {
  170.     };
  171.  
  172.     public void mousePressed(MouseEvent e) {
  173.         if ( SwingUtilities.isLeftMouseButton(e) ) {
  174.         AbstractButton b = (AbstractButton) e.getSource();
  175.         ButtonModel model = b.getModel();
  176.  
  177.         // But because of the mouseDragged hack above, we can't do setArmed
  178.         // in mouseEnter. As a workaround, set it here just before setting
  179.         // focus.
  180.         model.setArmed(true);
  181.         model.setPressed(true);
  182.         if(!b.hasFocus()) {
  183.         b.requestFocus();
  184.         }            
  185.     }
  186.     };
  187.     
  188.     public void mouseReleased(MouseEvent e) {
  189.     AbstractButton b = (AbstractButton) e.getSource();
  190.     ButtonModel model = b.getModel();
  191.     model.setPressed(false);
  192.     };
  193.  
  194.     public void mouseEntered(MouseEvent e) {
  195.     AbstractButton b = (AbstractButton) e.getSource();
  196.     if(b.isRolloverEnabled()) {
  197.         b.getModel().setRollover(true);
  198.     }
  199.     };
  200.  
  201.     public void mouseExited(MouseEvent e) {
  202.     AbstractButton b = (AbstractButton) e.getSource();
  203.     if(b.isRolloverEnabled()) {
  204.         b.getModel().setRollover(false);
  205.     }
  206.     };
  207.     
  208.  
  209.     static class PressedAction extends AbstractAction {
  210.     AbstractButton b = null;
  211.         PressedAction(AbstractButton b) {
  212.         super("pressedAction");
  213.         this.b = b;
  214.     }
  215.     
  216.     public void actionPerformed(ActionEvent e) {
  217.         ButtonModel model = b.getModel();
  218.         model.setArmed(true);
  219.         model.setPressed(true);
  220.         if(!b.hasFocus()) {
  221.         b.requestFocus();
  222.         }
  223.         }
  224.  
  225.     public boolean isEnabled() {
  226.         if(!b.getModel().isEnabled()) {
  227.         return false;
  228.         } else {
  229.         return true;
  230.         }
  231.     }
  232.     }
  233.  
  234.    static class ReleasedAction extends AbstractAction {
  235.     AbstractButton b = null;
  236.         ReleasedAction(AbstractButton b) {
  237.         super("releasedAction");
  238.         this.b = b;
  239.     }
  240.     
  241.     public void actionPerformed(ActionEvent e) {
  242.         b.getModel().setPressed(false);
  243.         }
  244.  
  245.     public boolean isEnabled() {
  246.         if(!b.getModel().isEnabled()) {
  247.         return false;
  248.         } else {
  249.         return true;
  250.         }
  251.     }
  252.     }
  253.  
  254. }
  255.  
  256.